package com.atguigu.gmall.order.handler.manager;

import com.atguigu.gmall.order.handler.AbstractOrderHandler;
import com.atguigu.gmall.order.dto.OrderSubmitDto;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class OrderHandlerManager implements ApplicationContextAware {

    // 定义list集合管理订单操作的所有的步骤
    private List<AbstractOrderHandler> orderHandlerList = new ArrayList<>() ;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        Map<String, AbstractOrderHandler> orderHandlerMap = applicationContext.getBeansOfType(AbstractOrderHandler.class);
        orderHandlerList = orderHandlerMap.values().stream().sorted((o1, o2) -> o1.sort() - o2.sort()).collect(Collectors.toList());
        for(int x = 0 ; x < orderHandlerList.size() ; x++) {   // 构建链表结构
            if(x == orderHandlerList.size() - 1) {
                orderHandlerList.get(x).setNext(null);
            }else {
                orderHandlerList.get(x).setNext(orderHandlerList.get(x + 1));
            }
        }
    }

    // 责任链步骤的执行入口
    public String exec(OrderSubmitDto orderSubmitDto , String tradeNo) {
        return orderHandlerList.get(0).process(orderSubmitDto , tradeNo) ;
    }
}
